package com.woniu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.ResultCom;
import com.woniu.entity.News;
import com.woniu.mapper.AdminMapper;
import com.woniu.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news2")
public class News2Controller {

    @Autowired
    INewsService iNewsService;

    @Autowired
    AdminMapper adminMapper;

    @PostMapping("/add")
    public ResultCom addNews(@RequestBody News news){
       Boolean success =   iNewsService.save(news);
       return success ? ResultCom.success("添加成功") : ResultCom.error("添加失败");
    }

    @PostMapping("/update")
    public ResultCom updateN(@RequestBody News news){
        Boolean success = iNewsService.updateById(news);
        return success ? ResultCom.success("添加成功") : ResultCom.error("添加失败");
    }

   @GetMapping("/detail")
   public ResultCom detail(@RequestParam int id){
      News NewsData =  iNewsService.getNewsDetail(id);

       return NewsData!=null ? ResultCom.success("获取成功", NewsData) : ResultCom.error("获取失败");
   }

   @DeleteMapping("/delete")
    public  ResultCom delete(@RequestParam int id){
      Boolean success =  iNewsService.removeById(id);
       return success ? ResultCom.success("删除成功") : ResultCom.error("删除失败");
   }

   @DeleteMapping("/delete/more")
    public  ResultCom deleteMre(@RequestParam List<Integer> ids){
       Boolean success = iNewsService.removeByIds(ids);
//       iNewsService.removeBatchByIds(ids);  // 过时了
       return success ? ResultCom.success("删除成功") : ResultCom.error("删除失败");
   }

   @GetMapping("/list")
   public ResultCom getList(@RequestParam(value = "pageIndex", defaultValue = "1", required = true) int pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "0", required = true) int pageSize ){

       Page<News>  datas =  iNewsService.getNewsList(pageIndex, pageSize);
       return  datas!=null ? ResultCom.success("获取成功", datas) : ResultCom.error("获取失败");
   }
}
